package com.base.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import tool.JsonTool;
import tool.ReadUrl;

import com.base.bean.*;
import com.base.domain.*;
import com.base.dao.*;
import com.hf.bean.*;


public class AddressService {
	//单例模式 start
	private volatile static AddressService instance = null;	
	
	private AddressService() {
	}
	public static AddressService getInstance() {
		if (instance == null) {
			synchronized (AddressService.class) {// 1
				if (instance == null) {// 2
					instance = new AddressService();// 3
					
				}
			}
		}
		return instance;
	}
	//单例模式 end
	
	// 由经纬度获取详细地名
	public String getNameByGps(String longitude ,String latitude){
		System.out.println(longitude+"----"+latitude);
//		System.out.println(ReadUrl.getNameByGps(longitude, latitude));
//		return null;
		return ReadUrl.getNameByGps(longitude, latitude);
	}
	
	
	

	
	//1--由省获取市    2--由市获取县
	public List<SimpleAddress> getAddress(int type,String fatherId){
		List<SimpleAddress> adds=new ArrayList<SimpleAddress>();
		adds.add(new SimpleAddress("",""));
		if(type==1){
			List<AddCity> citys=getCitysByCode(fatherId);
			for(AddCity c:citys){
				adds.add(new SimpleAddress(c.getCity(),c.getCityId()));
			}
			return adds;
		}
		if(type==2){
			List<AddArea> areas=getAreasByCode(fatherId);
			for(AddArea a:areas){
				adds.add(new SimpleAddress(a.getArea(),a.getAreaId()));
			}
			return  adds;
		}else 
			return null;
		
		
		
	}
	
	
	
	// 获取所有省份 4省排序
	public List<AddProvince> getAllProvinces(){
		AddProvinceDao pd = (AddProvinceDao)_BaseDao.ac.getBean("AddProvinceDao");
		return pd.findByHql("from AddProvince a order by a.myorder");
	}
	
	
		
	
	
	
	// 由省代码获取所属市
	public List<AddCity> getCitysByCode(String code){	
		AddCityDao cd = (AddCityDao)_BaseDao.ac.getBean("AddCityDao");	
		return cd.findByHql("from AddCity as c where c.father='"+code+"'");
	}
	
	
	
	
	// 由市代码获取所属区县
	public List<AddArea> getAreasByCode(String code){	
		AddAreaDao ad = (AddAreaDao)_BaseDao.ac.getBean("AddAreaDao");
		return ad.findByHql("from AddArea as a where a.father='"+code+"'");

	}
	
	
	
	public enum ADD_TYPE {
        PROVINCE,CITY,AREA
    }
	
	
	/**
	 * @param type 1-省  2-市 3-区域
	 * @param code
	 * @return
	 */	
	public String getNameByCode(ADD_TYPE addType,String code){
		AddProvinceDao pdao=(AddProvinceDao)_BaseDao.ac.getBean("AddProvinceDao");	//用于获取session
		AddCityDao cdao = (AddCityDao)_BaseDao.ac.getBean("AddCityDao");
		AddAreaDao adao = (AddAreaDao)_BaseDao.ac.getBean("AddAreaDao");
		String name="";
		switch(addType){
		case PROVINCE:
			name = pdao.findByHql("from AddProvince where provinceId='"+code+"'").get(0).getProvince();
			break;
		case CITY:	
			name=cdao.findByHql("from AddCity where cityId='"+code+"'").get(0).getCity();
			break;
		case AREA:
			name=adao.findByHql("from AddArea where areaId='"+code+"'").get(0).getArea();
			break;
		}
		
		return name;
	}
	
	// 获取所有省
	public List<SimpleAddress> getAddress(){
		List<SimpleAddress> adds=new ArrayList<SimpleAddress>();
		adds.add(new SimpleAddress("",""));
		List<AddProvince> ps= getAllProvinces();
		for(AddProvince p:ps){
			adds.add(new SimpleAddress(p.getProvince(),p.getProvinceId()));
		}
		return adds;
	}
	
// TEST	
public static void main(String[] args) throws Exception{
	
//	AddressService a=AddressService.getInstance();
//	 System.out.println(JsonTool.obj2json(a.getAllProvinces("")));
//	 System.out.println(a.getNameByCode(ADD_TYPE.AREA, "110101"));
	AddressService.getInstance().getNameByGps("20", "30");
	
	
	
}

}
